﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace CheckList
{
    public partial class CheckListPage : UserControl{

        private  int _adviceCount = 0;

        public CheckListPage(List<CheckListItem> items, string title)
        {
            InitializeComponent();
            Program.Workspace.ColumnsChanged +=new Action<List<string>>(Workspace_ColumnsChanged);
            HideColumns();
            _bindWorkNodeItems.DataSource = items;
            dgvWorkArea.DataSource = _bindWorkNodeItems;
            lblTitle.Text = title;
            _adviceCount = Program.Workspace.GetAdviceCount(title);
        }



        public void LockCells(){
            int count = GetDefaultCount(lblTitle.Text);
            for (int i = 0; i < count; i++){
                dgvWorkArea[1, i].ReadOnly = true;
            }
        }

        private int GetDefaultCount(string s){
            int count = 0;
            switch (s)
            {
                case "本质安全审查":
                    count = Program.Workspace.IDefaultCount; break;
                case "人为因素审查":
                    count = Program.Workspace.PDefaultCount; break;
                case "设施布置审查":
                    count = Program.Workspace.FDefaultCount; break;
                default: break;
            }
            return count;
        }

        private void HideColumns(){
            foreach (DataGridViewColumn column in dgvWorkArea.Columns)
            {
                if (Program.Workspace.GetHideColumns().Contains(column.HeaderText))
                {
                    column.Visible = false;
                }
                else
                {
                    column.Visible = true;
                }
            }
        }

        private void Workspace_ColumnsChanged(List<string> hideCols){
            foreach (DataGridViewColumn column in dgvWorkArea.Columns)
            {
                if (hideCols.Contains(column.HeaderText))
                {
                    column.Visible = false;
                }
                else
                {
                    column.Visible = true;
                }
            }
        }

        private void dgvWorkArea_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {   
            int col = dgvWorkArea.CurrentCell.ColumnIndex;
            int row = dgvWorkArea.CurrentCell.RowIndex;
            if(dgvWorkArea.CurrentCell.OwningColumn.Name == "advice" && dgvWorkArea.CurrentCell.Value != null&&!dgvWorkArea.CurrentCell.Value.Equals("")){
                if (dgvWorkArea.Rows[row].Cells[col - 1].Value.Equals(""))
                {
                    dgvWorkArea.Rows[row].Cells[col - 1].Value = GetAdviceType(lblTitle.Text) + "--" + Program.Workspace.GetAdviceCount(lblTitle.Text);
                }
            }
            if (dgvWorkArea.CurrentCell.OwningColumn.Name == "advice" && dgvWorkArea.CurrentCell.Value == null)
            {
                dgvWorkArea.Rows[row].Cells[col - 1].Value = "";
            }
        }

        private string GetAdviceType(string s){
            string res = string.Empty;
            switch (s){
                case "本质安全审查":
                    res = "I";break;
                case "人为因素审查":
                    res = "H";break;
                case "设施布置审查":
                    res = "F";break;
                default:break;
            }
            return res;
        }

        private void addNewRow_Click(object sender, EventArgs e)
        {   
            CheckListItem item = new CheckListItem((dgvWorkArea.Rows.Count+1).ToString(),"","","","","","","");
            _bindWorkNodeItems.Insert(dgvWorkArea.Rows.Count,item);
            dgvWorkArea.Rows[dgvWorkArea.Rows.Count - 1].Selected = true;
            dgvWorkArea.CurrentCell = dgvWorkArea[1, dgvWorkArea.Rows.Count - 1];
        }

        public void SaveWithoutRemider(){
           
        }

        private void CheckListPage_Load(object sender, EventArgs e)
        {
            LockCells();
        }


    }
}
